package com.carleasoft.mps.adu.business.basicsInfo.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.adu.business.basicsInfo.bean.po.AuthorityUserBasicInfoPo;
import com.carleasoft.mps.adu.business.basicsInfo.bean.po.ExcelPo;
import com.carleasoft.mps.adu.business.basicsInfo.bean.po.TargetItemPo;
import com.carleasoft.mps.adu.business.basicsInfo.bean.po.UserInfoCountPo;
import com.carleasoft.mps.adu.business.basicsInfo.bean.vo.AuthorityUserBasicInfoVo;
import com.carleasoft.mps.adu.business.basicsInfo.bean.vo.AuthorityUserInfoVo;
import com.carleasoft.mps.adu.business.basicsInfo.bean.vo.UserInfoCountVo;
import com.carleasoft.mps.adu.business.basicsInfo.service.AuthorityUserBasicInfoService;
import com.carleasoft.mps.adu.common.annotaion.OpImgAnnotation;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yangHao
 * @createDate:2023/6/5
 * @description:
 */
@RestController
@RequestMapping("/basic")
public class AuthorityUserBasicInfoController {

    @Autowired
    private AuthorityUserBasicInfoService authorityUserBasicInfoService;

    /**
     * 添加用户
     *
     * @param authorityUserBasicInfoPo
     * @return
     */
    @PostMapping("/saveUser")
    public R saveUser(@RequestBody AuthorityUserBasicInfoPo authorityUserBasicInfoPo) {
        boolean isOk = authorityUserBasicInfoService.saveUser(authorityUserBasicInfoPo);
        return R.ok(isOk);
    }

    /**
     * 修改用户
     *
     * @param authorityUserBasicInfoPo
     * @return
     */
    @PostMapping("/updateUser")
    public R updateUser(@RequestBody AuthorityUserBasicInfoPo authorityUserBasicInfoPo) {
        boolean isOk = authorityUserBasicInfoService.updateUser(authorityUserBasicInfoPo);
        return R.ok(isOk);
    }

    /**
     * 分页查询用
     *
     * @param authorityUserBasicInfoPo
     * @return
     */
    @PostMapping("/pageQuery")
    @OpImgAnnotation("headPicture")
    public R pageQuery(@RequestBody AuthorityUserBasicInfoPo authorityUserBasicInfoPo) {
        if (StrUtil.isNotBlank(authorityUserBasicInfoPo.getUserName())) {
            authorityUserBasicInfoPo.setKeyword(authorityUserBasicInfoPo.getUserName());
        }
        Page<AuthorityUserBasicInfoVo> authorityUserBasicInfoVoPage = authorityUserBasicInfoService.pageQuery(new Page(authorityUserBasicInfoPo.getCurrent(), authorityUserBasicInfoPo.getSize()), authorityUserBasicInfoPo);
        return R.ok(authorityUserBasicInfoVoPage);
    }

    /**
     * 根据userCode 查询详情
     *
     * @param authorityUserBasicInfoPo
     * @return
     */
    @PostMapping("/selectById")
    @OpImgAnnotation("headPicture")
    public R selectById(@RequestBody AuthorityUserBasicInfoPo authorityUserBasicInfoPo) {
        IBeanUtil.paramVerify(authorityUserBasicInfoPo, "userCode");
        AuthorityUserInfoVo authorityUserBasicInfo = authorityUserBasicInfoService.selectById(authorityUserBasicInfoPo.getUserCode());
        return R.ok(authorityUserBasicInfo);
    }

    /**
     * 删除用户
     *
     * @param authorityUserBasicInfoPo
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody AuthorityUserBasicInfoPo authorityUserBasicInfoPo) {
        IBeanUtil.paramVerify(authorityUserBasicInfoPo, "userCode");
        boolean isOk = authorityUserBasicInfoService.delete(authorityUserBasicInfoPo);
        return R.ok(isOk);
    }

    /**
     * 下载模板
     *
     * @return
     */
    @PostMapping("/excel/download")
    public R excel(HttpServletResponse response) {
        authorityUserBasicInfoService.excel(response);
        return R.ok();
    }

    /**
     * 导入用户数据
     *
     * @param file
     * @return
     */
    @PostMapping("/importUserExcel")
    public R importUserExcel(@RequestPart("file") MultipartFile file) {
        boolean isOk = authorityUserBasicInfoService.importUserExcel(file);
        return R.ok(isOk);
    }


    /**
     * 下载模板
     *
     * @return
     */
    @PostMapping("/excel/exam/download")
    public R excel(@RequestBody ExcelPo excelPo, HttpServletResponse response) {
        IBeanUtil.paramVerify(excelPo, "type");
        authorityUserBasicInfoService.excelExam(excelPo, response);
        return R.ok();
    }

    /**
     * 导入花名册数据
     *
     * @param
     * @return
     */
    @PostMapping("/importExcel")
    public R importExcel(@RequestPart("file") MultipartFile file) {
        List<TargetItemPo> targetItemPos = authorityUserBasicInfoService.importExcel(file);
        return R.ok(targetItemPos);
    }


    /**
     * 根据条件筛选用户
     *
     * @return
     */
    @PostMapping("/userInfoCount")
    public R userInfoCount(@RequestBody UserInfoCountPo userInfoCountPo) {
        Page<UserInfoCountVo> userInfoCountVoPage = authorityUserBasicInfoService.userInfoCount(new Page(userInfoCountPo.getCurrent(), userInfoCountPo.getSize()), userInfoCountPo);
        return R.ok(userInfoCountVoPage);
    }


    /**
     * 获取当前登录人提交权限标识
     *
     * @return
     */
    @PostMapping("/userSubmitFlagById")
    public R userSubmitFlagById() {
        return R.ok(authorityUserBasicInfoService.userSubmitFlagById());
    }

}
